<template>
	<view class="home">
		<!-- 1，定位 -->
		<home-addrss :address="address" @changeAddress="changeAddress"></home-addrss>
		<!-- 2，轮播图 -->
		<home-swiper :swiperList="swiperList" @gotoDetail="gotoDetail"></home-swiper>
		<!-- 3，标题导航 -->
		<home-tips :tips="tipsData"></home-tips>
		<!-- 4，功能导航 -->
		<home-function></home-function>
		<!-- 5，navbar导航 -->
		<home-navbar @changeActive="changeActive" :activeIndex="activeIndex"></home-navbar>
		<!-- 6，查询动态数据 -->
		<view class="list" v-for="(item,index) in homeList" :key="index">
			<detail-item :contentData="item"></detail-item>
		</view>

		<!-- 7，底部提示 -->
		<view class="bottom-tips">
			<text>没有更多信息...</text>
		</view>

		<!-- 8，弹框 -->
		<lyg-popup :policyPath="policyPath" :protocolPath="protocolPath" :showPopup="isShowPopup"
			@popupState="popupState" @confirm="confirm"></lyg-popup>
	</view>
</template>

<script>
	//1，导入子组件
	import homeAddrss from "./home-child/home-address.vue"
	import homeSwiper from './home-child/home-swiper.vue'
	import homeTips from './home-child/home-tips.vue'
	import homeFunction from './home-child/home-function.vue'
	import homeNavbar from './home-child/home-navbar.vue'
	//1.1，引入别的页面子组件（公用）
	import detailItem from '@/subpkg/main/home/release-detail/child/detail-item.vue'
	import lygPopup from '@/components/apk/lyg-popup/lyg-popup.vue'

	//2，导入本地js数据
	import {
		swiperListData
	} from '@/local_data/main/home.js'

	//3，引入网络请求
	import {
		queryByTypePage
	} from '@/api/http/main/release/dynamic.js'
	import {
		loginByVisitor
	} from '@/api/http/login/login.js'

	//4，引入vuex
	import {
		mapState,
		mapMutations,
		mapActions
	} from 'vuex'
	
	import websocketMix from '@/mixins/websocket_init.js'

	export default {
		components: {
			homeAddrss,
			homeSwiper,
			homeTips,
			homeFunction,
			homeNavbar,
			detailItem,
			lygPopup
		},
		mixins: [websocketMix],
		data() {
			return {
				addressObject: {},
				address: uni.getStorageSync('dwAddress') ||
					'',
				swiperList: swiperListData,
				tipsData: {
					name: '掌上社区'
				},
				activeIndex: 1,
				homeList: [],
				page: 1,
				size: 10,
				typeId: '1',
				isShowPopup: false,
				policyPath: '/subpkg/main/my/agree-on/private-xy/private-xy',
				protocolPath: '/subpkg/main/my/agree-on/user-xy/user-xy',
				isVisitorLoginData: '', //默认游客登录
				userToken: ''
			}
		},
		computed: {
			...mapState('m_user_info', ['userInfo', 'isVisitorLogin']),
		},
		methods: {
			//1，更改地址
			changeAddress(res) {
				this.addressObject = res
				this.address = res.name
				uni.setStorageSync('dwAddress', res.name)
			},
			//2，跳转到轮播图详情页面
			gotoDetail(res) {

			},
			//3，更换navbar导航选中
			changeActive(id) {
				this.activeIndex = id

				if (uni.getStorageSync('isVisitorLogin')) {
					//console.log('用户模式！')
					this.getDetailList(id)
				} else {
					//console.log('游客模式！')
					this.visitorLogin(id)
				}

			},

			//4，查询主要展示数据
			getDetailList(typeId) {
				//console.log('进入')
				var userId = uni.getStorageSync('userId');
				var req = {}
				if (!userId || userId == '') {
					req = {
						page: this.page,
						size: this.size,
						typeId: typeId,
						state: '1',
						isReleaseUser: 3
					}
				} else {
					req = {
						page: this.page,
						size: this.size,
						typeId: typeId,
						state: '1',
						isReleaseUser: 1,
						userId,
					}
				}

				queryByTypePage(req).then(res => {
					if (res.data.pubInfo.code !== 0) {
						return uni.$showMsg(res.data.pubInfo.message);
					}
					//console.log(res.data.busInfo)
					this.homeList = res.data.busInfo
				})
			},
			//5，展示弹框
			ifShowAlert() {
				if (!uni.getStorageSync('isShowAlert')) {
					this.isShowPopup = true
					uni.setStorageSync('isShowAlert', true)
				}
			},
			//5.2，确定隐私协议
			confirm() {
				//this.isChecked = true
				this.isShowPopup = false
			},
			//5.3，关闭隐私协议
			popupState() {
				this.isShowPopup = false
			},
			//6，游客登录
			visitorLogin(activeId) {
				var userToken = this.userToken
				if (userToken == '') {
					//console.log('没有token')
					loginByVisitor().then(res => {
						if (res.data.pubInfo.code !== 0) {
							return uni.$showMsg(res.data.pubInfo.message);
						}
						var busInfo = res.data.busInfo
						uni.setStorageSync('token', busInfo.token);
						uni.setStorageSync('isVisitorLogin', false);
						this.isVisitorLoginData = false
						this.userToken = busInfo.token
						//this.gotoHome();
						this.getDetailList(activeId)
					})
				} else {
					//console.log('有token')
					this.getDetailList(activeId)
				}

			}

		},
		onLoad(e) {

			/* var pag = getCurrentPages()
			console.log(pag) */

			this.ifShowAlert()
			//1，当前是游客登录
			/* if (uni.getStorageSync('isVisitorLogin')) {
				console.log('用户模式！')
				this.getDetailList('1')
			} else {
				console.log('游客模式！')
				this.visitorLogin()

			} */

		},
		onShow() {
			this.userToken = this.userInfo.token
			
			//console.log(this.userInfo)
			//this.activeIndex = 1
			//1，当前是游客登录
			//console.log(this.isVisitorLogin)
			this.isVisitorLoginData = this.isVisitorLogin
			if (this.isVisitorLoginData) {
				console.log('用户模式！')
				this.getDetailList(this.activeIndex)
			} else {
				console.log('游客模式！')
				this.visitorLogin()

			}
		}
	}
</script>

<style lang="scss" scoped>
	.home {
		min-height: 100vh;
		width: 100%;
		background-color: #F3F4F3;
	}

	.bottom-tips {
		height: 100rpx;
		line-height: 100rpx;
		text-align: center;

		text {
			color: #bfbfbf;
		}
	}
</style>